//
//  ZSMasonryScrollView.m
//  AllToolsOC
//
//  Created by safiri on 2017/8/4.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "ZSMasonryScrollView.h"

@interface ZSMasonryScrollView ()

@property (nonatomic ,strong) MASConstraint *contentViewWidthConstraint;
@property (nonatomic ,strong) MASConstraint *lastTrailingConstraint;
@property (nonatomic ,strong) MASConstraint *lastContentViewWidthConstraint;


@property (nonatomic ,assign) NSInteger allPageCount;
@end

@implementation ZSMasonryScrollView


- (void)addHorizontallyScrollViews:(NSArray <UIView *>*)views {
    
    NSInteger count = views.count;
    self.allPageCount = count;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (self.lastContentViewWidthConstraint) {
            [self.lastContentViewWidthConstraint uninstall];
        }
        self.lastContentViewWidthConstraint = make.width.equalTo(self).multipliedBy(count);
    }];
    for (NSInteger i=0; i<count; i++) {
        id view = views[i];
        [self.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i==0) {
                make.leading.equalTo(self.contentView.mas_leading);
            }else{
                make.left.equalTo(self.contentView.subviews[i-1].mas_right);
            }
            if (i==count-1) {
                self.lastTrailingConstraint = make.trailing.equalTo(self.contentView.mas_trailing);
            }
            
            make.top.equalTo(self.contentView.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom);
            make.width.equalTo(self);
        }];
    }
}

//此方法还无法使用 self.lastTrailingConstraint
- (void)addHorizontallyScrollView:(UIView *)view {
    self.allPageCount += 1;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (self.lastContentViewWidthConstraint) {
            [self.lastContentViewWidthConstraint uninstall];
        }
        self.lastContentViewWidthConstraint = make.width.equalTo(self).multipliedBy(self.allPageCount);
    }];
    
    [self.contentView addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.allPageCount==1) {
            make.leading.equalTo(self.contentView.mas_leading);
        }else{
            make.left.equalTo(self.contentView.subviews[self.allViewsCount-2].mas_right);
        }
        if (self.lastTrailingConstraint) {
            [self.lastTrailingConstraint uninstall];
        }
        self.lastTrailingConstraint = make.trailing.equalTo(self.contentView.mas_trailing);
        
        make.top.equalTo(self.contentView.mas_top);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.width.equalTo(self);
    }];
}

#pragma mark - get set

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [self addSubview:_contentView];
        
        if (self.isHorizontallyScroll) {
            [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
                make.centerY.equalTo(self.mas_centerY);
            }];
        }else {
            [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
                make.centerX.equalTo(self.mas_centerX);
            }];
        }
    }
    return _contentView;
}

- (NSInteger)allViewsCount {
    return self.contentView.subviews.count;
}
@end
